import { Component, OnInit, Output, EventEmitter, ViewChild, ViewContainerRef } from '@angular/core'
import { FormGroup, FormBuilder } from '@angular/forms'
// import { SearchComponent } from '../../components/search/search.component'
import { ModalService } from '@app/shared/utils'
import { ModalConfirmComponent } from '@app/shared/components/modal-confirm/modal-confirm.component'
import { NetService, PageService } from '@app/core/http'
import { ApiService } from '../../services/api.service'
import { QlMessageService } from 'qloud-angular'
import { Router } from '@angular/router'
import { CursorService } from '@app/core/http/cursor.service'
import { Store } from '@ngxs/store'
import { DictService } from '@app/shared/utils/dict.service'
import { ActivatedRoute } from '@angular/router'

@Component({
  selector: 'app-customer-list',
  templateUrl: './customer-list.component.html',
  styleUrls: ['./customer-list.component.css'],
  providers: [ModalService]
})
export class CustomerListComponent implements OnInit {
  public customerSex
  public customerName
  public customerType

  public queryParams: any = {}

  public formGroup: FormGroup
  public qformGroup: FormGroup

  public customerList: any[] = []

  public groupList: any[] = []
  public showGroup = false

  @ViewChild('addGroup', { static: true })
  public addGroupTemplate: ViewContainerRef

  @ViewChild('changeGroup', { static: true })
  public changeGroupTemplate: ViewContainerRef

  @ViewChild('advQuery', { static: true })
  public advQueryTemplate: ViewContainerRef

  public valuedata: any = ''

  constructor(
    private apiService: ApiService,
    private fb: FormBuilder,
    public modal: ModalService,
    public pageService: PageService,
    public dictService: DictService,
    public cursorService: CursorService,
    private message: QlMessageService,
    private router: Router,
    public route: ActivatedRoute,
    private store: Store
  ) {
    this.qformGroup = this.fb.group({
      customerName: [null],
      customerNo: [null],
      contact: [null],
      assetBal: [null],
      professionalTitle: [null],
      age: [null],
      aum: [null],
      wealthPotentialValue: [null],
      marriaged: [null],
      sex: [null],
      customerStar: [null],
      customerLevel: [null],
      firstOpenTime:[null]
    })
  }

  ngOnInit() {
    this.formGroup = this.fb.group({ valuedata: [''] })
    console.log(this.store.selectSnapshot(state => state.user))
    this.onRefresh()
  }

  public onRefresh() {
    // 并列调用 zip(this.apiService.getCustomerData(),this.apiService2.getCustomerData()).subscribe()
    // let sql = 'select *'
    if (this.customerSex != '' && this.customerSex != undefined) {
      this.cursorService.query().eq('sex',this.customerSex)
    }
    if (this.customerName != '' && this.customerName != undefined) {
      this.cursorService.query().eq('customername',this.customerName)
    }
    if (this.customerType != '' && this.customerType != undefined) {
      this.cursorService.query().eq('customertype',this.customerType)
    }
    this.getCustomer(this.cursorService)
  }

  public getCustomer(curcor) {
    this.apiService.getCustomerList(curcor).subscribe(data => {
      this.customerList = data
    })
    this.cursorService.query({reset:true})
  }

  // public transform(key: string, value: any, sql: string) {
  //   if (sql != 'select *') {
  //     sql += ' and ' + key + ' = ' + "'*" + value + "*'"
  //   } else {
  //     sql += ' where ' + key + ' = ' + "'*" + value + "*'"
  //   }
  //   return sql
  // }

  public queryH() {
    this.modal
      .open({
        title: '高级搜索',
        component: this.advQueryTemplate
      })
      .subscribe(() => {})
    // this.modal
    //   .open({
    //     size: 'large',
    //     title: '高级搜索',
    //     component:this.SearchTemplate,
    //     data: {}
    //   })
    //   .subscribe(() => {
    //     this.onRefresh()
    //     this.message.success(`共查询到${this.customerList.length}条结果`)
    //   })
  }

  public saveQuery() {
    // let sql = 'select *'
    if (this.qformGroup.value.customerName != '' && this.qformGroup.value.customerName != undefined) {
      this.cursorService.query().eq('customername',this.qformGroup.value.customerName)
    }
    if (this.qformGroup.value.customerNo != '' && this.qformGroup.value.customerNo != undefined) {
      this.cursorService.query().eq('customerno',this.qformGroup.value.customerNo)
    }
    if (this.qformGroup.value.contact != '' && this.qformGroup.value.contact != undefined) {
      this.cursorService.query().eq('contact',parseInt(this.qformGroup.value.contact))
      // sql = this.transformInt('contact', parseInt(this.qformGroup.value.contact), sql)
    }
    if (this.qformGroup.value.assetBal != '' && this.qformGroup.value.assetBal != undefined) {
      this.cursorService.query().eq('assetbal',parseInt(this.qformGroup.value.assetBal))
    }
    if (this.qformGroup.value.professionalTitle != '' && this.qformGroup.value.professionalTitle != undefined) {
      this.cursorService.query().eq('professionaltitle',parseInt(this.qformGroup.value.professionalTitle))
    }
    if (this.qformGroup.value.age != '' && this.qformGroup.value.age != undefined) {
      this.cursorService.query().eq('age',parseInt(this.qformGroup.value.age))
    }
    if (this.qformGroup.value.aum != '' && this.qformGroup.value.aum != undefined) {
      this.cursorService.query().eq('aum',parseInt(this.qformGroup.value.aum))
    }
    if (this.qformGroup.value.wealthPotentialValue != '' && this.qformGroup.value.wealthPotentialValue != undefined) {
      this.cursorService.query().eq('wealthpotentialvalue',parseInt(this.qformGroup.value.wealthPotentialValue))
    }
    if (this.qformGroup.value.marriaged != '' && this.qformGroup.value.marriaged != undefined) {
      this.cursorService.query().eq('marriaged',this.qformGroup.value.marriaged)
      // sql = this.transform('marriaged', this.qformGroup.value.marriaged, sql)
    }
    if (this.qformGroup.value.sex != '' && this.qformGroup.value.sex != undefined) {
      this.cursorService.query().eq('sex',this.qformGroup.value.sex)
      // sql = this.transform('sex', this.qformGroup.value.sex, sql)
    }
    if (this.qformGroup.value.customerStar != '' && this.qformGroup.value.customerStar != undefined) {
      this.cursorService.query().eq('customerstar',this.qformGroup.value.customerStar)
    }
    if (this.qformGroup.value.customerLevel != '' && this.qformGroup.value.customerLevel != undefined) {
      // sql = this.transform('customerLevel', this.qformGroup.value.customerLevel, sql)
      this.cursorService.query().eq('customerlevel',this.qformGroup.value.customerLevel)
    }
    if (this.qformGroup.value.firstOpenTime != '' && this.qformGroup.value.firstOpenTime != undefined) {
      // sql = this.transform('firstOpenTime', this.qformGroup.value.firstOpenTime, sql)
      this.cursorService.query().eq('firstopentime',this.qformGroup.value.firstOpenTime)
    }
    // this.cursorService.statement = sql
    this.modal.close()
    this.getCustomer(this.cursorService)
  }

  public onCheckChange(selection) {
    this.showGroup = selection.length > 0
    this.showGroup = true
  }

  public onCreateGroup() {
    this.modal
      .open({
        title: '创建分组',
        size: 'large',
        component: this.addGroupTemplate
      })
      .subscribe(() => {
        this.message.success('创建成功')
        this.router.navigateByUrl('/customer/customer-group')
      })
  }

  public onChangeGroup() {
    this.modal
      .open({
        title: '移动分组',
        size: 'large',
        component: this.changeGroupTemplate
      })
      .subscribe(() => {
        this.message.success('移动成功')
      })
  }

  /**
   * 删除方法
   *  */
  // public delFun() {
  //   this.modal
  //     .open({
  //       title: '确认删除',
  //       component: ModalConfirmComponent
  //     })
  //     .subscribe(() => {
  //       this.apiService.delCustomer().subscribe(data => {
  //         this.message.success(data[0].message)
  //       })
  //     })
  // }

  public jumpDeatail(ref: any): void {
    this.router.navigate(['/customer/customer-detail'], {
      queryParams: { code: ref.rowData.customerno, risk: ref.rowData.riskLevel }
    })
  }
}
